<!--#include file="..\CheckSession.inc"-->
<%
bID=Request("bID")
btendonvixuly=Request("btendonvixuly")
bdonvi=Request("bdonvi")
bcomm=Request("bcomm")
bghinhan=Request("bghinhan")
if bcomm="loaibo" then
	btendonvixuly=Cutmadonvi(btendonvixuly,bdonvi)
	sSQL="delete q from donvi as n inner join canbo as c on n.madonvi=c.madonvi inner join quyen as q on c.macanbo=q.macanbo where q.mavanthu like '"& bID &"' and n.tendonvi like '"& bdonvi &"'"
	Conn.Execute sSQL
	Set RecMDV=server.CreateObject("ADODB.Recordset")
		RecMDV.CursorType=3
		RecMDV.CacheSize=5
		sSQL="select madonvi from donvi where tendonvi like '"& bdonvi &"'"
		RecMDV.Open sSQL,Conn
		if RecMDV.recordcount > 0 then
			bmadonvi=trim(RecMDV("madonvi"))
		end if
	RecMDV.close
	set RecMDV=nothing
	sSQL="delete qn from donvi as n inner join quyen_nhom as qn on n.ID=qn.manhom where qn.mavanthu like '"& bID &"' and n.madonvi like '"& mid(bmadonvi,1,4)+"%" &"' and n.madonvi <> '"& bmadonvi &"'"
	Conn.Execute sSQL
end if
if bghinhan="update" then
	sSQL="Update Totrinh set DonviXL='"& Donvixuly(btendonvixuly) &"' Where ID ='"& bID &"'" 
	Conn.Execute sSQL
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="vi">
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="cache-control" CONTENT="private">
<title>Selab Net.Office</title>
<SCRIPT LANGUAGE=vbscript runat=server>
function Findmadonvi(strdonvixuly,strmadonvi)
	i = instr(1,strdonvixuly,strmadonvi)
	if i>0 then
		Findmadonvi=true
	else
		Findmadonvi=false
	end if
end function
function Cutmadonvi(strdonvixuly,strmadonvi)
	i=instr(1,strdonvixuly,strmadonvi)
	intlen=len(strmadonvi)
	intlendv=len(strdonvixuly)
	if intlen >= intlendv then
		Cutmadonvi=""
	else
		if i > 1 then
			Cutmadonvi=mid(strdonvixuly,1,i-3)+mid(strdonvixuly,i+intlen,len(strdonvixuly))
		else
			Cutmadonvi=mid(strdonvixuly,intlen+3,len(strdonvixuly)-intlen-2)
		end if
	end if
end function
Function Donvixuly(strTendonvixuly)
	check=true
	if strTendonvixuly="" then
		strdonvixuly=""
	else
		strdonvixuly="-"
	end if
	strdonvixulyloi=""
	intlong=len(strTendonvixuly)
	i=0
	i=instr(1,strTendonvixuly,",")
	j=1
	k=0
	l=0
	if i <=0 then
		Set RecDV=server.CreateObject("ADODB.Recordset")
		RecDV.CursorType=3
		RecDV.CacheSize=5
		sSQL="select madonvi from donvi where tendonvi like '"& strTendonvixuly &"'"
		RecDV.Open sSQL,Conn
		if RecDV.Recordcount then
			strdonvixuly="-"+RecDV("madonvi")+"M-"
		else
			strdonvixulyloi=strTendonvixuly
		end if
		RecDV.close
		set RecDV=nothing
	else
		do while check
			Set RecDV=server.CreateObject("ADODB.Recordset")
			RecDV.CursorType=3
			RecDV.CacheSize=5
			sSQL="select madonvi from donvi where tendonvi like '"& mid(strTendonvixuly,j,i-j) &"'"
			RecDV.Open sSQL,Conn
			if RecDV.Recordcount then
				if l=0 then
					strdonvixuly=strdonvixuly+RecDV("madonvi")+"M-"
				else
					strdonvixuly=strdonvixuly+RecDV("madonvi")+"S-"
				end if
			else
				strdonvixulyloi=strdonvixulyloi+mid(strTendonvixuly,j,i-j)+","
			end if
			RecDV.close
			set RecDV=nothing
			k=instr(i+1,strTendonvixuly,",")
			j=i+2
			if k <=0 then
				check=false
				Set RecDV=server.CreateObject("ADODB.Recordset")
				RecDV.CursorType=3
				RecDV.CacheSize=5
				sSQL="select madonvi from donvi where tendonvi like '"& mid(strTendonvixuly,j,intlong-j+1) &"'"
				RecDV.Open sSQL,Conn
				if RecDV.Recordcount then
					strdonvixuly=strdonvixuly+RecDV("madonvi")+"S-"
				else
					strdonvixulyloi=strdonvixulyloi+mid(strTendonvixuly,j,intlong-j+1)+","
				end if
				RecDV.close
				set RecDV=nothing
			else
				check=true
			end if
			i=k
			l=l+1
		loop
	end if
	if strdonvixulyloi="" then
		Donvixuly=strdonvixuly
	else
		Donvixuly=strdonvixuly+"#"+strdonvixulyloi
	end if
End function
</script>
<script language=javascript>
function CmdThoat_onclick()
{
	window.close();
}
function CmdThemmoi_onclick()
{
	checknhom=frm.nhom.value;
	if (checknhom=="")
	{
		return false;
	}
	else
	{
	dvxl=frm.bdonvixuly.value;
	if (dvxl=="")
		{
		donvixuly=frm.nhom.value;
		}
		else
		{
		donvixuly=dvxl+", "+frm.nhom.value;
		}
	window.frm.action="AddDonviXL.asp?btendonvixuly="+donvixuly+"&bmacanbo="+"<%=bmacanbo%>"+"&bID="+"<%=bID%>"+"&bghinhan=update";
	window.frm.submit();
	}
}
function CmdLoaibo_onclick()
{
	donvixuly=frm.bdonvixuly.value;
	donvi=frm.donvi.value;
	if (donvi=="")
	{
		return false;
	}
	else
	{
	window.frm.action="AddDonviXL.asp?btendonvixuly="+donvixuly+"&bdonvi="+donvi+"&bcomm=loaibo"+"&bmacanbo="+"<%=bmacanbo%>"+"&bID="+"<%=bID%>"+"&bghinhan=update";
	window.frm.submit();
	}
}
function loaibo_update()
{
window.opener.document.Noidung.TxtDonviXL.value="<%=btendonvixuly%>";
}
</script>
</head>
<body topmargin=0 leftmargin=0 bgcolor=#BDD5D0 onload="loaibo_update()">
	<form name=frm method=post>
		<input type=hidden name="bdonvixuly" value="<%=btendonvixuly%>">
		<table width=100% border=0 cellpadding="1" cellspacing="1">
			<tr>
				<td width=40%>
					<select name="nhom" size="14" style="font-family: times new roman; width: 100%; border-style: solid; border-width: 1; padding-left: 0; background-color: #FFFFFF;" LANGUAGE=javascript ondblclick="return CmdThemmoi_onclick()">
					<%
					sSql="select tendonvi from donvi where madonvi like '00%00' and madonvi <> '000000' order by madonvi"
					Set RecNhom=Server.CreateObject("ADODB.Recordset")
					RecNhom.CursorType=3
					RecNhom.CacheSize=5
					RecNhom.Open sSQL,Conn
					RecNhom.movefirst
					do while not RecNhom.eof
						if not Findmadonvi(btendonvixuly,RecNhom("tendonvi")) then
						%>
							<option value="<%=RecNhom("tendonvi")%>"><%=RecNhom("tendonvi")%></option>
						<%
						end if
					RecNhom.movenext
					loop
					RecNhom.close
					set RecNhom=nothing
					%>
					</select>
				</td>
				<td width=20% align=center>
					<Button name="Themmoi" style="font-family: Arial;width: 80%"  LANGUAGE=javascript onclick="CmdThemmoi_onclick()" >--></Button>
					<Button name="Loaibo" style="font-family: Arial;width: 80%"  LANGUAGE=javascript onclick="CmdLoaibo_onclick()" ><--</Button>
				</td>
				<td width=40%>
					<select name="donvi" size="14" style="font-family: times new roman; width: 100%; border-style: solid; border-width: 1; padding-left: 0; background-color: #FFFFFF;" LANGUAGE=javascript ondblclick="return CmdLoaibo_onclick()">
					<%
						check=true
						j=0
						k=0
						i=0
						do while check
							j=instr(j+1,btendonvixuly,",")
							k=j
							if k > 0 then
								strtendonvixuly=mid(btendonvixuly,i+1,j-i-1)
								check=true
								%>
								<option value="<%=strtendonvixuly%>"><%=strtendonvixuly%></option>
								<%
							else
								strtendonvixuly=mid(btendonvixuly,i+1,len(btendonvixuly)-i)
								%>
								<option value="<%=strtendonvixuly%>"><%=strtendonvixuly%></option>
								<%
								check=false
							end if
							k=0
							i=j+1
						loop
					%>
					</select>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>